﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Collections;

namespace DAO
{
    public class NguoiTimViec_DAO
    {
        private static QLTDTVDataContext dt = new QLTDTVDataContext();


        public static void GetRole(string UserName, ref string User_ID, ref string Role)
        {
            var user = dt.spGetRole(UserName);
            foreach (var u in user)
            {
                User_ID = u.User_ID.ToString();
                Role = u.Name;
            }
        }

        public static void LuuCongViecDaXem(int UserID, int Mcv, DateTime ngaygio)
        {
            dt.spLuuCongViecDaXem(UserID, Mcv, ngaygio);
        }
        public static void HuyCongViecDaXem(int UserID, int Mcv)
        {
            dt.spXoaCongViecDaXem(UserID, Mcv);
        }

        public static IEnumerable LayThongTinCVOnline(string UserID)
        {
            return dt.spLayThongTinCVOnline(UserID).AsEnumerable();
        }

        //public static List<CVONLINE> LayThongTinCVOnline(string UserID)
        //{
        //    List<CVONLINE> dscvonline = new List<CVONLINE>();
        //    var list = dt.spLayThongTinCVOnline(UserID);
        //    foreach (var cv in list)
        //    {
        //        CVONLINE cvonline = new CVONLINE();
        //        cvonline.DiaChi = cv.DiaChi;
        //        cvonline.DiDong = cv.DiDong;
        //        cvonline.DienThoai = cv.DienThoai;
        //        cvonline.Email = cv.Email;
        //        cvonline.GioiThieu = cv.GioiThieu;
        //        cvonline.GioiTinh = (bool)cv.GioiTinh;
        //        cvonline.HoTen = cv.HoTen;
        //        cvonline.MucLuong = Convert.ToDecimal(cv.MucLuong);
        //        cvonline.NamKinhNghiem = cv.NamKinhNghiem;
        //        cvonline.NgaySinh = cv.NgaySinh;
        //        cvonline.TrinhDo = cv.TrinhDo;
        //        cvonline.ViTriMongMuon = cv.ViTriMongMuon;

        //        dscvonline.Add(cvonline);
        //    }

        //    return dscvonline;
        //}

        public static int GuiCVOnline(int UserID, int maNhaTuyenDung)
        {
            try
            {
                string flag = "";
                dt.spGuiCVOnline(UserID, maNhaTuyenDung, DateTime.Now, ref flag);

                return int.Parse(flag);
            }
            catch
            {
                return 0;
            }
        }


        public static bool Register(NguoiTimViecDTO nguoiTimViec)
        {
            try
            {
                string flag = "";
                string password = MyMembershipProvider.ToHashString(nguoiTimViec.PassWord);
                dt.spDangKyNguoiTimViec(nguoiTimViec.UserName, password, nguoiTimViec.Date.ToString(), nguoiTimViec.Ten, nguoiTimViec.Ho, nguoiTimViec.GioiTinh, nguoiTimViec.Email,
                                    nguoiTimViec.NgaySinh, nguoiTimViec.DienThoai, nguoiTimViec.Didong, DateTime.Now.Date, nguoiTimViec.DiaChi, nguoiTimViec.MaTinhThanh, ref flag);

                if (flag == "0")
                    return false;
                else
                    return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static NguoiTimViecDTO LayDanhSachThongTinNguoiTVTheoUserID(int user_id)
        {
            try
            {
                var danhSachThongTin = dt.spLayThongTinNguoiTimViecTheoUserID(user_id);

                NguoiTimViecDTO nguoiTimViec = new NguoiTimViecDTO();

                foreach (var ds in danhSachThongTin)
                {
                    if (ds.MaTinhThanh != null)
                        nguoiTimViec.MaTinhThanh = (int)ds.MaTinhThanh;
                    nguoiTimViec.UserName = ds.USERNAME;
                    nguoiTimViec.Ten = ds.Ten;
                    nguoiTimViec.Ho = ds.Ho;
                    nguoiTimViec.GioiTinh = bool.Parse(ds.GioiTinh.ToString());
                    nguoiTimViec.Email = ds.Email;
                    nguoiTimViec.NgaySinh = DateTime.Parse(ds.NgaySinh.ToString());
                    nguoiTimViec.DienThoai = ds.DienThoai;
                    nguoiTimViec.Didong = ds.DiDong;
                    nguoiTimViec.DiaChi = ds.DiaChi;

                }

                return nguoiTimViec;
            }
            catch (Exception)
            {
                return null;
            }


        }

        public static bool CapNhatThongTinNguoiTimViec(int user_id, NguoiTimViecDTO nguoiTimViec)
        {
            try
            {
                string flag = "";
                dt.spUpdateNguoiTimViec(user_id, nguoiTimViec.Ten, nguoiTimViec.Ho, nguoiTimViec.GioiTinh, nguoiTimViec.Email, nguoiTimViec.NgaySinh, nguoiTimViec.DienThoai, nguoiTimViec.Didong, nguoiTimViec.DiaChi, nguoiTimViec.MaTinhThanh, ref flag);

                if (flag == "1")
                    return true;
                else
                    return false;
            }
            catch (Exception)
            {
                return false;
            }


        }

        public static void NhanThongTinTuNhaTuyenDung(string MaNguoiTimViec, string MaNhaTuyenDung, ref string Flag)
        {
            dt.spNhanThongTinTuNhaTuyenDung(MaNguoiTimViec, MaNhaTuyenDung, DateTime.Now, ref Flag);
        }

        public static List<NHATUYENDUNG> DanhSachCacNhaTuyenDungDangKyNhanThongTin(int UserID)
        {
            List<NHATUYENDUNG> listNTD = new List<NHATUYENDUNG>();
            var list = dt.spDanhSachCacNhaTuyenDungDangKyNhanThongTin(UserID);
            foreach (var ntt in list)
            {
                NHATUYENDUNG ntd = new NHATUYENDUNG();
                ntd.MaNhaTuyenDung = ntt.MaNhaTuyenDung;
                ntd.Ten = ntt.TenNhaTuyenDung;
                ntd.NgayDangKi = ntt.NgayDangKi;
                listNTD.Add(ntd);
            }
            return listNTD;
        }

        public static void HuyNhanThongTinTuNhaTuyenDung(int UserID, int Mct)
        {
            dt.spHuyNhanThongTinTuNhaTuyenDung(UserID, Mct);
        }


        //new update 

        public static int KiemTraDongPhiCuaNguoiTimViec(int userID)
        {
            try
            {
                string flag = "";
                dt.spKiemTraDongPhiCuaNguoiTimViec(userID, ref flag);

                if (flag == "0")
                    return 0;
                else
                    return 1;
            }
            catch (Exception)
            {
                return 2;
            }
        }

        public static bool GuiCVUploadChoNhaTuyenDung(int userID, int maNhaTuyenDung, string tenCV, string duongDan)
        {
            try
            {
                string flag = "";
                dt.spCVUploadGuiNhaTuyenDung(userID, maNhaTuyenDung, tenCV, duongDan, ref flag);

                if (flag == "1")
                    return true;
                else
                    return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static void CapNhatCVOnline(CVONLINE cvonline, string UserID)
        {
            dt.spCapNhatCVOnline(UserID, cvonline.HoTen, cvonline.GioiTinh, cvonline.Email, cvonline.DienThoai, cvonline.DiDong, cvonline.MaTinhThanh, cvonline.TrinhDo, cvonline.NamKinhNghiem, cvonline.DiaChi, Convert.ToDouble(cvonline.MucLuong), cvonline.ViTriMongMuon, cvonline.GioiThieu);
            dt.SubmitChanges();
        }

        //new update 
        public static IEnumerable LayThongTinPhiVaNguoiTimViec()
        {
            return dt.spLayThongTinPhiVaNguoiTimViec();
        }

        public static IEnumerable TimKiemPhiVaNguoiTimViec(string tuKhoa)
        {
            return dt.spTimKiemPhiVaNguoiTimViec(tuKhoa);
        }

        public static bool CapNhatPhiNguoiTimViec(NguoiTimViec_Phi_DTO nguoiTimViecPhi, int tinhTrang)
        {
            try
            {
                string flag = "";
                dt.spCapNhatPhiNguoiTimViec(nguoiTimViecPhi.MaNguoiTimViec, nguoiTimViecPhi.MaPhi, nguoiTimViecPhi.NgayBD, nguoiTimViecPhi.HinhThucThanhToan, tinhTrang, ref flag);

                if (flag == "1")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static NguoiTimViec_Phi_DTO LayThongTinPhiNguoiTimViecTheoMaNTVVaMaPhi(int maNguoiTimViec, int maPhi)
        {
            NguoiTimViec_Phi_DTO nguoiTimViecPhi = new NguoiTimViec_Phi_DTO();

            var ds = dt.spLayThongTinPhiNguoiTimViecTheoMaNguoiTimViecVaMaPhi(maNguoiTimViec, maPhi);
            foreach (var item in ds)
            {
                nguoiTimViecPhi.MaNguoiTimViec = item.MaNguoiTimViec;
                nguoiTimViecPhi.MaPhi = item.MaPhi;
                nguoiTimViecPhi.NgayBD = (DateTime)item.NgayBatDau;
                nguoiTimViecPhi.NgayKT = (DateTime)item.NgayKetThuc;
                nguoiTimViecPhi.HinhThucThanhToan = item.HinhThucThanhToan;
            }

            return nguoiTimViecPhi;
        }

        public static void BaoSaiPham(int Mcv)
        {
            dt.spBaoSaiPham(Mcv);
        }
    }
}
